extends CharacterBody2D

var hp = 1
var hit_effects = preload("res://scenes/hit_effects.tscn")
var smoke_effects = preload("res://scenes/smoke_effects.tscn")
var controller
var sound
var player

func _ready():
	controller = get_node("/root/Controller")	
	player = controller.get_player()
	sound = get_node("/root/SoundManager")
	get_node("Hitbox").connect("body_entered", _on_Hitbox_body_enter)

func _on_Hitbox_body_enter( body ):
	if (body.getDamageType() == "attack"):
		_get_hit()
		sound.play_sfx("hit",true)
		var controller = get_node("/root/Controller")
		var map = controller.current_map
		var instance = hit_effects.instantiate()
		var player = controller.get_player()
		map.add_child(instance)
		instance.play()
		instance.global_position  = global_position-Vector2(0,16) #-16 + 32*player.sees_left
		instance.scale = Vector2(player.top_sprite.scale.x,1)
		if (hp <= 0):
			_death()

func _death():
	var instance = smoke_effects.instantiate()
	var player = controller.get_player()
	controller.current_map.add_child(instance)
	instance.play()
	instance.global_position = global_position - Vector2(0,16)
	instance.scale = Vector2(player.top_sprite.scale.x,1)
	sound.play_sfx("smoke",true)
	queue_free()

func _get_hit():
	hp -= 1
